﻿package com.edward.service;


import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.SL_Cargo;
import com.edward.model.ShoppingList;


public class SL_CargoManager extends HibernateDaoSupport {
	
	private JdbcTemplate jdbcTemplate;
	
	

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public void addSL_Cargo(SL_Cargo sl_Cargo) {
		
		getHibernateTemplate().save(sl_Cargo);
	}
	
	@SuppressWarnings("unchecked")
	public PagerModel pagerSLCargo(int start,int limit,int id) {
		List <SL_Cargo>list = getHibernateTemplate().find("from SL_Cargo s where SLCargo_ID = " + id );
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <SL_Cargo>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	public void updateSL_Cargo(SL_Cargo sl_Cargo) {
		
		getHibernateTemplate().update(sl_Cargo);
	}
	
	public SL_Cargo getSL_Cargo(int id){
		SL_Cargo s =(SL_Cargo)getHibernateTemplate().get(SL_Cargo.class, id);
		return s;
	}

	public void deleteSL_Cargo(int id){
		
		getHibernateTemplate().delete(getSL_Cargo(id));
	}
	
	@SuppressWarnings("unchecked")
	public boolean check(int id){
		List <SL_Cargo>list = getHibernateTemplate().find("from SL_Cargo s where CARGO_ID = " + id );
		
		if(list.size() == 1 && list.get(0).getCargo().getSign().equals("sl")){
			return true;
		}else{
			return false;
		}
		
	}
	
	@SuppressWarnings("unchecked")
	public ShoppingList getSL(int id){
		String sql = "select  s.SLCargo_ID as id from sl_cargo s where s.id = " + id ;
		Map<String,Integer> m  = jdbcTemplate.queryForMap(sql);
		int i =(Integer) m.get("id");
		ShoppingList s =(ShoppingList)getHibernateTemplate().get(ShoppingList.class, i);
		
		return s;
		
		
	}



}



